package com.xcity.game.employee.config.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.xcity.db.entity.Employee;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.employee.config.AbstractConfigure;
import com.xcity.game.employee.config.Configure;
import com.xcity.game.employee.config.EmployeeServiceConfigure;
import com.xcity.game.employee.template.EmployeeQualityUpTemplate;
import com.xcity.game.employee.template.EmployeeStarUpTemplate;
import com.xcity.game.item.ItemInfo;
import com.xcity.game.item.ItemService;
import com.xcity.game.item.ItemTemplate;
import com.xcity.game.skill.template.SkillLevelUpTemplate;
import com.xcity.game.skill.v2.DefaultSkillV2;
import com.xcity.game.skill.v2.SkillManagerV2;

public class EmployeeRebornConfigure extends AbstractConfigure {
	
	public EmployeeRebornConfigure(Configure parent) {
		super(parent);
	}

	@Override
	public EmployeeRebornConfigure configure() {
		return this;
	}
	
	/**
	 * 获取员工重生钻石消耗
	 * @param employee
	 * @return
	 */
	public int getRebornStone(Employee employee) {
		int delta = GameStaticConfig.rebornCostStoneDelta;
		// ( ( 等级 - 1 ) x 0.1 + 品级 + 星级 ) x 变量
		int val = (int) (((employee.getLevel() - 1) * 0.1f + employee.getQualityLvl() + employee.getStarLvl()) * delta);
		val = val / 10 * 10; // 舍掉个位数
		return val > 0 ? val : delta;
	}
	
	/**
	 * 获取员工重生可获得物品
	 * @param employee
	 * @return
	 */
	public List<ItemInfo> getRebornReturns(Employee employee) {
		if (!employee.isTrained()) {
			return Collections.emptyList();
		}
		List<ItemInfo> returns = new ArrayList<>();
		// 等级->经验书
		statExpBooks(employee, returns);
		// 品级->潮人卡、宝石、现金
		long gold = statQualityLvlItems(employee, returns);
		// 星级->达人卡、员工卡、现金
		gold += statStarLvlItems(employee, returns);
		// 技能->技能点、现金
		gold += statSkillsItems(employee, returns);
		if (gold > 0) {
			returns.add(new ItemInfo(ItemService.goldDefault, (int) gold));
		}
		return returns;
	}
	
	private void statExpBooks(Employee employee, List<ItemInfo> returns) {
		int exp = (int) (employee.getExp() * GameStaticConfig.rebornReturnRatio);
		List<ItemTemplate> templates = ((EmployeeServiceConfigure) parent).getCommonConfigure().getExpBooks();
		int oldSize = returns.size();
		for (int i = templates.size() - 1; i >= 0; i--) {
			ItemTemplate template = templates.get(i);
			int val = template.getParameter(0);
			int count = exp / val;
			if (count > 0) {
				returns.add(new ItemInfo(template, count));
				exp -= val * count;
			}
			if (exp <= 0) {
				break;
			}
		}
		if (returns.size() == oldSize) {
			// 给一本最低的经验书
			returns.add(new ItemInfo(templates.get(0), 1));
		}
	}
	
	private long statQualityLvlItems(Employee employee, List<ItemInfo> returns) {
		if (employee.getQualityLvl() <= 0) {
			return 0;
		}
		List<EmployeeQualityUpTemplate> templates = ((EmployeeServiceConfigure) parent).getQualityLevelUpConfigure().getTemplates(employee.getTemplate().getUpQualityId());
		Map<Integer, ItemInfo> map = new LinkedHashMap<>();
		long gold = 0;
		for (int i = employee.getQualityLvl() - 1; i >= 0; i--) {
			EmployeeQualityUpTemplate template = templates.get(i);
			ItemInfo[] items = template.getWrappedItems();
			for (ItemInfo item : items) {
				Integer id = item.getId();
				ItemInfo val = map.get(id);
				if (val == null) {
					val = new ItemInfo(item.getTemplate(), 0);
					map.put(id, val);
				}
				val.setCount(val.getCount() + item.getCount());
			}
			gold += template.getGold();
		}
		float ratio = GameStaticConfig.rebornReturnRatio;
		// 物品
		Collection<ItemInfo> vals = map.values();
		for (ItemInfo val : vals) {
			int count = (int) (val.getCount() * ratio);
			if (count < 1) {
				count = 1;
			}
			val.setCount(count);
			returns.add(val);
		}
		// 现金
		return (long) (gold * ratio);
	}
	
	private long statStarLvlItems(Employee employee, List<ItemInfo> returns) {
		if (employee.getStarLvl() <= 0) {
			return 0;
		}
		EmployeeCommonConfigure commonConfigure = ((EmployeeServiceConfigure) parent).getCommonConfigure();
		List<EmployeeStarUpTemplate> templates = ((EmployeeServiceConfigure) parent).getStarLevelUpConfigure().getTemplates(employee.getTemplate().getId());
		Map<Integer, ItemInfo> map = new LinkedHashMap<>();
		Map<Integer, ItemInfo> cards = new LinkedHashMap<>(2);
		long gold = 0;
		for (int i = employee.getStarLvl() - 1; i >= 0; i--) {
			EmployeeStarUpTemplate template = templates.get(i);
			// 物品
			Integer id = template.getItemId();
			ItemInfo val = map.get(id);
			if (val == null) {
				val = new ItemInfo(template.getItemId(), 0);
				map.put(id, val);
			}
			val.setCount(val.getCount() + template.getItemCount());
			
			// 员工卡
			if (template.getMainCardCount() > 0) {
				// 主卡
				ItemTemplate it = commonConfigure.getEmployeeItem(employee.getTemplate().getId());
				val = cards.get(it.getId());
				if (val == null) {
					val = new ItemInfo(it, 0);
					cards.put(it.getId(), val);
				}
				val.setCount(val.getCount() + template.getMainCardCount());
			}
			if (template.getViceCardId() > 0 && template.getViceCardCount() > 0) {
				// 副卡
				ItemTemplate it = commonConfigure.getEmployeeItem(template.getViceCardId());
				val = cards.get(it.getId());
				if (val == null) {
					val = new ItemInfo(it, 0);
					cards.put(it.getId(), val);
				}
				val.setCount(val.getCount() + template.getViceCardCount());
			}
			
			// 现金
			gold += template.getGold();
		}
		
		float ratio = GameStaticConfig.rebornReturnRatio;
		// 物品
		Collection<ItemInfo> vals = map.values();
		for (ItemInfo val : vals) {
			int count = (int) (val.getCount() * ratio);
			if (count < 1) {
				count = 1;
			}
			val.setCount(count);
			returns.add(val);
		}
		// 员工卡 返还100%
		returns.addAll(cards.values());
		
		return (long) (gold * ratio);
	}
	
	private long statSkillsItems(Employee employee, List<ItemInfo> returns) {
		int gold = 0;
		SkillManagerV2 skm = employee.getSkillManager();
		int skps = skm.getActive().getLevel() - 1; // 技能点数
		gold += statSkillGold(skm.getActive());
		List<DefaultSkillV2> passives = skm.getPassives();
		for (int i = 0; i < passives.size(); i++) {
			DefaultSkillV2 passive = passives.get(i);
			skps += passive.getLevel() - 1;
			gold += statSkillGold(passive);
		}
		if (skps > 0) {
			// 技能点 返还100%
			returns.add(new ItemInfo(ItemService.addSkillPointsDefault, skps));
		}
		return (long) (gold * GameStaticConfig.rebornReturnRatio);
	}
	
	private long statSkillGold(DefaultSkillV2 skill) {
		int gold = 0;
		for (int lvl = skill.getLevel(); lvl > 1; lvl--) {
			SkillLevelUpTemplate template = skill.getTemplate().getUpLvlTemplate(lvl);
			gold += template.getGold();
		}
		return gold;
	}

}
